#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Master

#include <boost/test/unit_test.hpp>
#include "../DataStructures/Resource.h"

using boost::unit_test::test_suite;

BOOST_AUTO_TEST_SUITE(ResourceSuite)

BOOST_AUTO_TEST_CASE(basicTest)
{
	Resource* resource = new Resource(1, 1, CR);

	BOOST_CHECK(resource->addOwner(2, CR));
	BOOST_CHECK(resource->addOwner(3, CW));
	resource->removeOwner(3);
	BOOST_CHECK(resource->addOwner(4, PR));
	resource->removeOwner(4);
	BOOST_CHECK(resource->addOwner(5, PW));
	resource->removeOwner(5);
	BOOST_CHECK(!resource->addOwner(6, EX));

	BOOST_CHECK(resource->getOwnersSize() == 2);
	BOOST_CHECK(resource->getWaitingSize() == 1);

	for (int i = 1; i <= 5; i++)
		resource->removeOwner(i);

	BOOST_CHECK(resource->update() == 6);

	resource->removeOwner(6);

	BOOST_CHECK(resource->addOwner(1, CW));
	BOOST_CHECK(resource->addOwner(2, CR));
	BOOST_CHECK(resource->addOwner(3, CW));
	BOOST_CHECK(!resource->addOwner(4, PR));
	BOOST_CHECK(!resource->addOwner(5, PW));
	BOOST_CHECK(!resource->addOwner(6, EX));

	BOOST_CHECK(resource->getOwnersSize() == 3);
	BOOST_CHECK(resource->getWaitingSize() == 3);

	resource->removeOwner(3);
	resource->removeOwner(1);
	BOOST_CHECK(resource->getOwnersSize() == 1);
	BOOST_CHECK(resource->update() == 4);
	resource->removeWaiting(6);
	BOOST_CHECK(resource->update() == -1);
	BOOST_CHECK(resource->getOwnersSize() == 2);
	BOOST_CHECK(resource->getWaitingSize() == 1);

	for (int i = 1; i <= 3; i++)
		resource->removeOwner(i);

	BOOST_CHECK(resource->addOwner(1, PR));
	BOOST_CHECK(resource->addOwner(2, CR));
	BOOST_CHECK(resource->addOwner(3, PR));
	BOOST_CHECK(!resource->addOwner(4, CW));
	BOOST_CHECK(!resource->addOwner(5, PW));
	BOOST_CHECK(!resource->addOwner(6, EX));

	BOOST_CHECK(resource->addOwner(3, CR));
	BOOST_CHECK(!resource->addOwner(3, EX));

	for (int i = 1; i <= 3; i++)
		resource->removeOwner(i);

	BOOST_CHECK(resource->addOwner(1, PW));
	BOOST_CHECK(resource->addOwner(2, CR));
	BOOST_CHECK(!resource->addOwner(3, PR));
	BOOST_CHECK(!resource->addOwner(4, CW));
	BOOST_CHECK(!resource->addOwner(5, PW));
	BOOST_CHECK(!resource->addOwner(6, EX));

	for (int i = 1; i <= 3; i++)
		resource->removeOwner(i);

	BOOST_CHECK(resource->addOwner(1, EX));
	BOOST_CHECK(!resource->addOwner(2, CR));
	BOOST_CHECK(!resource->addOwner(3, PR));
	BOOST_CHECK(!resource->addOwner(4, CW));
	BOOST_CHECK(!resource->addOwner(5, PW));
	BOOST_CHECK(!resource->addOwner(6, EX));
}

BOOST_AUTO_TEST_SUITE_END()
